package com.binance.healthcenter;

import com.binance.healthcenter.common.RestTemplateErrorHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.telegram.telegrambots.ApiContextInitializer;

import com.binance.master.constant.Constant;
import com.binance.master.utils.IPUtils;
import com.wenfex.common.rest.HttpClientFactory;

import de.codecentric.boot.admin.config.EnableAdminServer;

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@EnableAdminServer
@EnableFeignClients(basePackages = {"com.binance.messaging.api"})
@SpringCloudApplication
public class HealthCenterApplication {

    public static void main(String[] args) {
        ApiContextInitializer.init();
        System.setProperty(Constant.LOCAL_IP, IPUtils.getIp());
        SpringApplication.run(HealthCenterApplication.class, args);
    }

    @Bean("healthCheckRestTemplate")
    public RestTemplate createRestTemplate() {
        HttpClientFactory httpClientFactory = new HttpClientFactory();
        httpClientFactory.setPollDefaultMaxPerRoute(200);
        httpClientFactory.setPollMaxTotal(400);
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpComponentsClientHttpRequestFactory.setConnectTimeout(15 * 1000);
        httpComponentsClientHttpRequestFactory.setReadTimeout(15 * 1000);
        httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(15 * 1000);
        httpComponentsClientHttpRequestFactory.setHttpClient(httpClientFactory.create());
        RestTemplate template = new RestTemplate(httpComponentsClientHttpRequestFactory);
        template.setErrorHandler(new RestTemplateErrorHandler());
        return template;
    }

}
